package com.jyu.apts.service.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.jyu.apts.dao.ItemBatchRecordRepository;
import com.jyu.apts.entity.ItemBatchRecord;
import com.jyu.apts.service.ItemBatchRecordService;

@Service
public class ItemBatchRecordServiceImpl implements ItemBatchRecordService {
	
	@Autowired
	ItemBatchRecordRepository itemBatchRecordDao;
	
	@Override
	public ItemBatchRecord getOne(long id) {
		return itemBatchRecordDao.findById(id).get();
	}

	@Override
	public Page<ItemBatchRecord> getItemBatchRecordPage(Integer pageNumber, Integer pageSize, Long itemProductionLineId,Integer status, String search) {
		if(pageNumber == null || pageNumber <= 0) {
			pageNumber = 1;
		}
		if(pageSize == null || pageSize <= 0) {
			pageSize = 10;
		}
		Sort sort = new Sort(Sort.Direction.ASC,"id");
		PageRequest pageRequest = new PageRequest(pageNumber-1,pageSize,sort);
		Specification<ItemBatchRecord> spec = new Specification<ItemBatchRecord>() {
			@Override
			public Predicate toPredicate(Root<ItemBatchRecord> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate pre = criteriaBuilder.conjunction();
				if(itemProductionLineId != null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("itemProductionLine").get("id"), itemProductionLineId.longValue()));
				}
				if(status!=null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("status"), status));
				}
				if(search!=null && search.trim().length()!=0) {
					Predicate or_pre = criteriaBuilder.disjunction(); 
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("itemProductionLine").get("name"), "%"+search+"%"));
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("itemProductionLine").get("item").get("name"), "%"+search+"%"));
					pre = criteriaBuilder.and(pre,or_pre);
				}
				return pre;
			}
			
		};
		return itemBatchRecordDao.findAll(spec, pageRequest);
	}

	@Override
	public void save(ItemBatchRecord itemBatchRecord) {
		itemBatchRecordDao.saveAndFlush(itemBatchRecord);
	}

	@Override
	public void save(List<ItemBatchRecord> itemBatchRecords) {
		itemBatchRecordDao.saveAll(itemBatchRecords);
		itemBatchRecordDao.flush();
	}

	@Override
	public void delete(List<ItemBatchRecord> itemBatchRecords) {
		itemBatchRecordDao.deleteAll(itemBatchRecords);
	}

	@Override
	public void delete(ItemBatchRecord itemBatchRecord) {
		itemBatchRecordDao.delete(itemBatchRecord);
	}

	@Override
	public List<ItemBatchRecord> findAll() {
		return itemBatchRecordDao.findAll();
	}

	@Override
	public List<ItemBatchRecord> findAllById(List<Long> ids) {
		return itemBatchRecordDao.findAllById(ids);
	}

}
